package com.yx.front.converter;

import cn.hutool.core.date.DateUtil;
import com.yx.front.config.global.CommonResult;
import com.yx.front.manager.CommentsReplyManager;
import com.yx.front.model.CommentsInfo;
import com.yx.front.model.param.CommentsParam;
import com.yx.front.model.vo.CommentsInfoVo;
import com.yx.front.model.vo.CommentsReplyVo;
import com.yx.front.enums.TimeFormat;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author YangXiong
 */
@Component
public class CommentsInfoConverter {
    @Resource
    private CommentsReplyManager commentsReplyManager;
    public List<CommentsInfoVo> handlerResult(List<CommentsInfo> records) {
        List<CommentsInfoVo> vos = new ArrayList<>();
        records.forEach(one->{
            CommentsInfoVo vo = new CommentsInfoVo();
            vo.setId(one.getId());
            vo.setLikeNum(one.getLikeNum());
            vo.setContent(one.getContent());
            vo.setFromAvatar(one.getFromAvatar());
            vo.setFromId(one.getFromId());
            vo.setFromName(one.getFromName());
            vo.setOwnerId(one.getOwnerId());
            vo.setType(one.getType());
            String format = DateUtil.format(one.getUpdateTime(), TimeFormat.TIME_FORMAT);
            vo.setUpdateTime(format);
            setReply(one, vo);
            vos.add(vo);
        });
        return vos;
    }

    public void setReply(CommentsInfo one, CommentsInfoVo vo) {
        CommentsParam param = new CommentsParam();
        param.setCurrentPage(1);
        param.setTotalPage(10);
        param.setMainId(one.getId());
        CommonResult<List<CommentsReplyVo>> pageByMainId = commentsReplyManager.getPageByMainId(param);
        List<CommentsReplyVo> data = pageByMainId.getData();
        vo.setReply(data);
    }
}
